<?

/*
 * Menu TEMPLATE (meta sensitive & with subs)
 * 
 * renders a menu extended meta values.
 * 
 * Paramaters:
 * -	Extra value
 *
 */

global $Engine;

$map = $Engine->map->filter();


foreach( $map as $node ) {
	global $Page;
	
	$active = ( $node['name'] == PAGE || $node['id'] == $Page->get( 'parent' ))? "class='active'" : "";
	
	$menu[] = "<li $active>" . meta_options( $node, $node[ 'title' ]) .  submenu( $node ) . "</li>";

}


function meta_options( $node, $content ) {
	global $map;
	
	$Pge = new PAGE( $id = (int) $node['id'] );
				
	if( $meta = $Pge->get( 'meta' ))
		if ( $meta->noclick ) {
    		$return = "<a>";
					
		} else if ( $meta->todesc ) {
   			
			$return = "<a href='/" . $node['name'] . "/" . $node['subs'][ 0 ]['name'] . "'>";
					
		} else if ( $meta->alias ) {
   			if( $parent = (int) $_SESSION['map']['num'][$meta->alias]['parent'] ) $parent = "/" . $_SESSION['map'][$parent]['name'];
			$return = "<a href='$parent/" . $_SESSION['map']['num'][$meta->alias]['name'] . "'>";
					
		}
				
	return ( ( !$return )? "<a href='/". $node['name'] ."'>" : $return ) . "$content</a>";
}


function submenu( $node ) {

	if( $node['subs'] ){
		foreach( $node['subs'] as $sub ) {
			
			if( PAGE == $sub['name'] ) $active = " active";
			$subs .= "<li" . (( PAGE == $sub['name'])? " class='active'" : "" ) . "><a href='{$sub['uri']}'>{$sub['title']}</a>".(( $sub['subs'] )? submenu( $sub ): "" )."</li>";
		}	
		
		return "<ul class='sub$active'>$subs</ul>";
	}
}


if( is_array( $template_param ))
	
	if( key( $template_param ) == "replace" )
		foreach( $template_param[ "replace" ] as $n => $repl )
			if( $repl ) $menu[ $n ] = "<li><a href='/" . $map[ $n ]['name'] . "?fresh=lang:nl'>$repl</a></li>";
				
		

?>

<ul id='menu'><?= implode( "\n", $menu ) ?></ul>